package com.woniu.woniu_bx.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.woniu.woniu_bx.exception.HelperCateNameIsExistException;
import com.woniu.woniu_bx.mapper.HelperCateMapper;
import com.woniu.woniu_bx.pojo.HelperCate;
import com.woniu.woniu_bx.pojo.PageBean;
import com.woniu.woniu_bx.service.HelperCateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 帮助中心类别service实现类
 * @author sjy
 * @create 2022-08-27 16:23
 */
@Service
@Transactional
public class HelperCateServiceImpl extends ServiceImpl<HelperCateMapper, HelperCate> implements HelperCateService {
    @Autowired
    private HelperCateMapper helperCateMapper;

    /**
     * 增加
     * 类别名不重复
     * @param helperCate
     */
    @Override
    public void add(HelperCate helperCate){
       if(helperCateMapper.selectNameNum(helperCate.getName())==1){
           throw new HelperCateNameIsExistException("帮助中心类别名已存在");
        }
       helperCateMapper.insert(helperCate);
    }

    /**
     * 修改
     * 类别名不重复
     * @param helperCate
     */
    @Override
    public void update(HelperCate helperCate){
        int id = helperCate.getId();
        HelperCate oldHelperCate = helperCateMapper.selectById(id);
        if(oldHelperCate.getName().equals(helperCate.getName())){
            return;
        }
        if(helperCateMapper.selectNameNum(helperCate.getName())==1){
            throw new HelperCateNameIsExistException("帮助中心类别名已存在");
        }
        helperCateMapper.updateById(helperCate);
    }

    /**
     * 分页查询
     * @param pageBean
     * @return
     */
    @Override
    public PageBean<HelperCate> getCateByPage(PageBean pageBean) {
        Page p = PageHelper.startPage(pageBean.getCurrPage(), pageBean.getPageSize());
        List<HelperCate> result = helperCateMapper.getAll();
        pageBean.setData(result);
        pageBean.setTotalPages(p.getPages());
        pageBean.setCurrPageSize(result.size());
        pageBean.setTotalNums((int)p.getTotal());
        return pageBean;
    }

    @Override
    public List<HelperCate> getAllCate() {
        List<HelperCate> allCate = helperCateMapper.getAllCate();
        return allCate;
    }
}
